package com.dh.bm.view;

import com.dh.bm.Application;
import com.dh.bm.controller.BookController;
import com.dh.bm.controller.OrderController;
import com.dh.bm.controller.UserController;
import com.dh.bm.entity.Book;
import com.dh.bm.entity.User;
import com.dh.bm.entity.vo.BookVo;

import java.util.List;
import java.util.Scanner;

public class TestDemo {
    private static BookController bookController = new BookController();
    private static UserController userController = new UserController();
    private static OrderController orderController = new OrderController();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("1.登录");
            System.out.println("2.添加书");
            System.out.println("3.查看所有书籍");
            System.out.println("4.根据ID查看书籍");
            System.out.println("5.借书");
            System.out.println("6.还书");
            System.out.println("7.更新书籍");
            System.out.println("请输入功能序号：");
            int num = scanner.nextInt();
            if (num == 1) {
                System.out.println("请输入用户名");
                String account = scanner.next();
                System.out.println("请输入密码");
                String passWord = scanner.next();
                User user = userController.login(account, passWord);
                System.out.println(user);
                if (user != null) {
                    Application.uid = user.getUid();// 把用户ID保存全局
                }
            } else if (num == 2) {
                System.out.println("请输入书名");
                String name = scanner.next();
                boolean res = bookController.addBook(name, "不二", 100, 1);
                System.out.println(res ? "成功" : "失败");
            } else if (num == 3) {
                List<Book> books = bookController.queryAll();
                for (Book book : books) {
                    System.out.println(book);
                }
            } else if (num == 4) {
                System.out.println("请输入书的ID");
                int id = scanner.nextInt();
                BookVo book = bookController.queryById(id);
                System.out.println(book);
            } else if (num == 5) {
                if (Application.uid == 0) {
                    System.out.println("请先登陆");
                } else {
                    System.out.println("请输入书的ID");
                    int id = scanner.nextInt();
                    boolean b = orderController.borrow_book(id, Application.uid);
                    System.out.println(b ? "成功" : "失败");
                }
            } else if (num == 7) {
                List<Book> books = bookController.queryAll();
                for (Book book : books) {
                    System.out.println(book);
                }
                System.out.println("请输入要更新数据ID");
                int id = scanner.nextInt();
                Book book = null;// 创建书籍空对象
                for (Book b : books) {
                    if (b.getBid() == id) {
                        book = b;
                    }
                }
                if (book != null) {
                    System.out.println("请输入要更新的字段序号");
                    while (true) {
                        System.out.println("1、更新书名");
                        System.out.println("2、更新作者");
                        System.out.println("3、退出更新");
                        int count = scanner.nextInt();
                        if (count == 1) {
                            String name = scanner.next();
                            book.setName(name);// 改成新的名字
                        } else if (count == 2) {
                            String author = scanner.next();
                            book.setAuthor(author);// 改成新的作者
                        } else {
                            break;
                        }
                    }
                    System.out.println(book);
                    boolean res = bookController.updateBookById(id, book.getName(), book.getAuthor(), book.getStock());
                    System.out.println(res ? "成功" : "失败");
                }


            }

        }


    }
}
